from utils.myjwt import varify_token
from django.http import JsonResponse

def check_login(viewfunc):
    def wrapper(request,*args,**kwargs):
        token = request.META.get('HTTP_AUTHORIZATION')
        print('*****',token)
        if token and token.startswith('Bearer'):
            token = token.split(' ')[1]
            print('>>>>>>',token)
            rest, v = varify_token(token)
            if not v:
                return JsonResponse({'msg':rest,'code':401},safe=False)
            else:
                # token验证成功后，设置一下当前登录用户,登录之后直接获取request.user也可以
                request.user = rest.get('user_username')
                return viewfunc(request,*args,**kwargs)
        else: 
            return JsonResponse({'msg':'请先登录','code':401},safe=False)
    return wrapper
